/**
 * 可以在其他的js文件中使用更新的js语法
 */
require('babel-core/register');
require('babel-polyfill');

//初始化
require('./util/setup')

var mongoose = require('mongoose')
// var uri = 'mongodb://search:search@127.0.0.1:12001/search_console'; //使用这个用户创建索引
var uri = 'mongodb://user:123@127.0.0.1:12001/search_console'; //使用这个用户增删改查
mongoose.connect(uri, {
    useMongoClient: true,
    /* other options */
});
mongoose.Promise = global.Promise;

/**
 * 定义一个服务器
 */
var Koa = require('koa');
var app = new Koa();

/**
 * 模版
 */
var Pug = require('koa-pug');
var pug = new Pug({
    viewPath: 'views', //pug模版存放的目录
    basedir: 'views', //extends使用绝对路径时
    app: app // equals to pug.use(app) and app.use(pug.middleware)
});

/**
 * 配置静态访问
 * 只有app/disk目录是可以直接访问的，这样用于引用前端生成的js和css文件
 */

var serve = require('koa-static');
app.use(serve("static"));


/**
 *  路由
 */
var Router = require('koa-router');
var router = new Router();
var server_url = require('./constants/server_url.js');


var getRouter = require('./api/get');
//post请求直接传json
var postRouter = require('./api/post');
var json = require('koa-json');

for (var key in server_url) {
    var mem = server_url[key];
    if (typeof(mem) === 'function') {
        router.get(mem()[0], getRouter)
        router.post(mem()[0], json(), postRouter);
    } else {
        router.get(mem, getRouter)
        router.post(mem, json(), postRouter);
    }
}
app.use(router.routes());
app.use(router.allowedMethods());


/*
 * 启动服务器
 */
app.listen(8081, () => {
    console.log('serve run on 8081');
})
